package com.ssy.lingxi.contract.esign.api;


import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.constant.RequestMethod;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import org.apache.commons.collections4.MapUtils;

import java.util.*;

/**
 * API请求类
 * @author yzc
 * @version 2.0.0
 * @date 2020/8/26
 */
public class EsignRequest {
    private RequestMethod httpMethod;
    private String resourcePath;
    private Map<String, List<String>> headerMap;
    private Map<String, Object> paramMap;
    private AuthToken authToken;
    private EsignProperties properties;

    public EsignRequest(RequestMethod httpMethod, String resourcePath, Map<String, Object> paramMap, EsignProperties properties) {
        this.httpMethod = httpMethod;
        this.resourcePath = resourcePath;
        this.paramMap = paramMap;
        this.properties = properties;
    }

    public EsignRequest(RequestMethod httpMethod, String resourcePath, Map<String, List<String>> headerMap, Map<String, Object> paramMap, AuthToken authToken, EsignProperties properties) {
        this.httpMethod = httpMethod;
        this.resourcePath = resourcePath;
        this.headerMap = headerMap;
        this.paramMap = paramMap;
        this.authToken = authToken;
        this.properties = properties;
    }

    public EsignRequest(RequestMethod httpMethod, String resourcePath, Map<String, Object> paramMap, AuthToken authToken, EsignProperties properties) {
        this.httpMethod = httpMethod;
        this.resourcePath = resourcePath;
        this.paramMap = paramMap;
        this.authToken = authToken;
        this.properties = properties;
    }

    public Map<String, List<String>> getNewTokenHeaderMap(boolean flag) throws EsignException {
        if (MapUtils.isEmpty(headerMap)) {
            headerMap = new HashMap<>();
        }

        if (flag){
            headerMap.put("X-Tsign-Open-App-Id", Collections.singletonList(properties.getAppId()));
            headerMap.put("X-Tsign-Open-Token", Collections.singletonList(authToken.getTokenAndCache()));
        }

        if (Objects.nonNull(authToken) && !flag) {
            headerMap.put("X-Tsign-Open-App-Id", Collections.singletonList(properties.getAppId()));
            headerMap.put("X-Tsign-Open-Token", Collections.singletonList(authToken.getCacheToken()));
        }

        return headerMap;
    }

    public Map<String, List<String>> getHeaderMap() throws EsignException {
        return getNewTokenHeaderMap(false);
    }

    public RequestMethod getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(RequestMethod httpMethod) {
        this.httpMethod = httpMethod;
    }

    public String getResourcePath() {
        return resourcePath;
    }

    public void setResourcePath(String resourcePath) {
        this.resourcePath = resourcePath;
    }

    public void setHeaderMap(Map<String, List<String>> headerMap) {
        this.headerMap = headerMap;
    }

    public Map<String, Object> getParamMap() {
        return paramMap;
    }

    public void setParamMap(Map<String, Object> paramMap) {
        this.paramMap = paramMap;
    }

    public AuthToken getAuthToken() {
        return authToken;
    }

    public void setAuthToken(AuthToken authToken) {
        this.authToken = authToken;
    }

    public EsignProperties getProperties() {
        return properties;
    }

    public void setProperties(EsignProperties properties) {
        this.properties = properties;
    }
}
